﻿package com.saint.bitmapDistortion 
{
	import com.saint.bitmapDistortion.animations.IStretchyAnimation;
	import com.saint.bitmapDistortion.animations.SuckbottomMiddle;
	import com.saint.bitmapDistortion.animations.SuckToPoint;
	import com.saint.bitmapDistortion.events.ImageAnimatorEvent;
	import com.saint.utils.Logger;
	import flash.events.EventDispatcher;
	/**
	 * class controlling the predefined animations of the stretcy images
	 * 
	 * @author kembera
	 */
	public class StretchyImageAnimator extends EventDispatcher
	{
		
		public static const SUCK_BOTTOM_MIDDLE:String  = "StretchyImageAnimator suck bottom middle";
		public static const SUCK_TO_POINT:String = "StretchyImageAnimator suck to piont";
	
		
		private var animation:IStretchyAnimation;
		
		public function StretchyImageAnimator() 
		{
			Logger.info("StretchyImageAnimator");			
		}

		
		/**
		 * sorts which animation we need -
		 * 
		 * bad bad data object!
		 * 
		 * @param	image
		 * @param	animationType
		 * @param	data
		 */
		public function animateImage(image:IStretchyImage, animationType:String, data:Object= null):void
		{
			Logger.info("StretchyImageAnimator.animateImage");
			switch(animationType)
			{
				case SUCK_BOTTOM_MIDDLE:
					animation = new SuckbottomMiddle(image);	
					startAnimation();
				break;
				
				case SUCK_TO_POINT:
					if (data.point == null)
					{
						throw new Error("StretchyImageAnimator.animateImage TO USE 'SuckToPoint' ANIMATION YOU MUST PASS THE TARGET POINT IN THE DATA OBJECT");
					}				
					animation = new SuckToPoint(image, data.point, data.time);
					animation.addEventListener(ImageAnimatorEvent.ANIMATION_COMPLETE, onAnimationComlete, false, 0, true);
					
				break;
				
				
				default:
					throw new Error("StretchyImageAnimator.animateImage UNKNOWN IMAGE TYPE");
				break;
			}
		}
		
		private function onAnimationComlete(e:ImageAnimatorEvent):void 
		{
			removeEventListener(ImageAnimatorEvent.ANIMATION_COMPLETE, onAnimationComlete);
			dispatchEvent(new ImageAnimatorEvent(ImageAnimatorEvent.ANIMATION_COMPLETE));
		}
		
		
		public function startAnimation():void
		{
			animation.startAnimation();
		}
		
		
		
	}

}